import type { AxiosResponse } from 'axios';
import { ElMessage } from 'element-plus';
import type { AppRequestConfig } from './type';
import HttpService from './core/http-service';
import DownloadService from './core/download-service';
import config from '@/config';
const baseOptions = {
  timeout: config.api.timeout,
  withCredentials: false,
  headers: config.api.commonHeaders
};
// 考古加
const kgjOptions = {
  ...baseOptions,
  baseURL: config.api.kgj
};

const kgjResponseHandler = (response: AxiosResponse<any, AppRequestConfig>) => {
  const { data } = response;
  let result = data;
  if (Reflect.has(data, 'result')) {
    result = data.result;
  }
  return { err: null, data: result, response };
};

export const { http: kgjHttp } = new HttpService(kgjOptions, kgjResponseHandler);

// 创意
const cyOptions = {
  ...baseOptions,
  baseURL: config.api.cy
};

const cyResponseHandler = (response: AxiosResponse<any, AppRequestConfig>) => {
  const { data } = response;
  return { err: null, data, response };
};

export const { http: cyHttp } = new HttpService(cyOptions, cyResponseHandler);

// 大赢加
const dyjOptions = {
  ...baseOptions,
  baseURL: config.api.dyj
};

const dyjResponseHandler = (response: AxiosResponse<any, AppRequestConfig>) => {
  const { data } = response;
  return { err: null, data, response };
};

export const { http: dyjHttp } = new HttpService(dyjOptions, dyjResponseHandler);

// 分销系统
const agencyOptions = {
  ...baseOptions,
  baseURL: config.api.agency
};

const agencyResponseHandler = (response: AxiosResponse<any, AppRequestConfig>) => {
  const { data } = response;
  let result = data;
  if (Reflect.has(data, 'result')) {
    result = data.result;
  }
  return { err: null, data: result, response };
};

export const { http: agencyHttp } = new HttpService(agencyOptions, agencyResponseHandler);
// 内容系统
const contentOptions = {
  ...baseOptions,
  baseURL: config.api.content
};

const contentResponseHandler = kgjResponseHandler;

export const { http: contentHttp } = new HttpService(contentOptions, contentResponseHandler);

// 下载
const downloadResponseHandler = (response: AxiosResponse<Blob, AppRequestConfig>, extraInfo: any) => {
  const { data } = response;

  let fileName = '';
  if (extraInfo && extraInfo.fileName) {
    fileName = extraInfo.fileName;
  } else {
    fileName = response.headers['content-disposition']
      ? decodeURI(
        response.headers['content-disposition'].split(';')[1].split('=')[1]
      )
      : '';
  }

  try {
    const type = response.headers['content-type'];
    let blob: Blob;
    if (type) {
      blob = new Blob([data], {
        type
      });
    } else {
      blob = new Blob([data]);
    }

    if ('msSaveBlob' in window.navigator) {
      (window.navigator as any).msSaveBlob(blob);
    } else {
      const url = window.URL.createObjectURL(blob);
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', fileName);
      link.style.display = 'none';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      // 释放内存
      window.URL.revokeObjectURL(link.href);
    }
    ElMessage.success('下载成功!');
  } catch (err) {
    console.error(err);
    ElMessage.error('下载失败!');
  }
};

export const { download: contentDownLoadHttp } = new DownloadService(contentOptions, downloadResponseHandler);
export const { download: agencyDownLoadHttp } = new DownloadService(agencyOptions, downloadResponseHandler);
export const { download: dyjDownLoadHttp } = new DownloadService(dyjOptions, downloadResponseHandler);
export const { download: cyDownLoadHttp } = new DownloadService(cyOptions, downloadResponseHandler);
export const { download: kgjDownLoadHttp } = new DownloadService(kgjOptions, downloadResponseHandler);
export { DownloadService };
